package com.zr.repertory.warelocation.controller;

import com.zr.config.ErrorMassage;
import com.zr.enums.StatusEnum;
import com.zr.model.*;
import com.zr.service.WareLocationService;
import com.zr.util.result.ResultBuilderUtil;
import com.zr.util.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/11/21.
 */
@RestController
@CrossOrigin
public class WareLocationController {

    @Autowired
    private WareLocationService wareLocationService;

    @PostMapping("getPullDownList")
    public ResultUtil getPullDownList(){
        //状态
        Map<String,List<PullDownVo>> satusMap = new HashMap<>();
        List<PullDownVo> pullDownVoList = new ArrayList<>();
        for (StatusEnum statusEnum : StatusEnum.values()) {
            pullDownVoList.add(new PullDownVo(statusEnum.getIndex(), statusEnum.getName()));
        }
        satusMap.put("status", pullDownVoList);
        return ResultBuilderUtil.success(satusMap);
    }

    //分页查询接口
    @PostMapping("StorageBin/queryPage")
    public ResultUtil queryPage(@RequestBody WareLocationSelectVo wareLocationSelectVo){
        return wareLocationService.queryPage(wareLocationSelectVo);
    }

    //修改前的回显
    @GetMapping("beforeupdate")
    public ResultUtil beforeUpdate(Integer wareLocationId){
        if (wareLocationId == null) {
            return  ResultBuilderUtil.error(ErrorMassage.QUERYBYID);    //修改的标识ID不存在
        }
        //如果没有错误跳到service层进行逻辑处理
        ResultUtil resultUtil = wareLocationService.beforeUpdate(wareLocationId);
        return  resultUtil;
    }

    //修改接口
    @PostMapping("update")
    public ResultUtil update(@RequestBody @Valid WareLocationUpdateVo wareLocationUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return wareLocationService.update(wareLocationUpdateVo);
    }



    //新增窗口-库房下拉框
    //查出库房信息
    @GetMapping("warehouse")
    public ResultUtil warehouse(){
        return wareLocationService.warehouse();
    }

    //新增窗口-库区下拉框
    //根据库房查出库区信息，返回一个list；根据库区查出库位信息，返回一个list
    @GetMapping("wareArea")
    public ResultUtil wareArea(@RequestBody @Valid QueryWareAreaByWarehouseVo queryWareAreaByWarehouseVo,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResultBuilderUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return wareLocationService.queryWareArea(queryWareAreaByWarehouseVo);
    }

    //新增  //需要修改
    @PostMapping("add")
    public ResultUtil save(@RequestBody @Valid WareLocationAddVo wareLocationAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return wareLocationService.save(wareLocationAddVo);
    }



}
